package com.framework.service.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.framework.common.Enums.FrameworkErrorCodeEnum;
import com.framework.common.exception.BusinessException;
import com.framework.common.response.ResponseBaseBean;
import com.framework.common.util.ValidateUtil;
import com.framework.service.core.biz.ITouristOrgBiz;
import com.framework.service.core.dao.mapper.TouristOrgInfoMapper;
import com.framework.service.core.dao.mapper.TouristOrgInfoSnapMapper;
import com.framework.service.core.service.ITouristOrgService;
import com.framework.core.api.request.TouristOrgSnapReq;
import com.framework.service.core.biz.ITouristOrgBiz;
import com.framework.service.core.dao.mapper.TouristOrgInfoMapper;
import com.framework.service.core.dao.mapper.TouristOrgInfoSnapMapper;
import com.framework.service.core.service.ITouristOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/5/26 22:49
 * @since JDK 1.8
 */
@Service
@Slf4j
public class TouristOrgServiceImpl implements ITouristOrgService {

    @Autowired
    private TouristOrgInfoSnapMapper touristOrgInfoSnapMapper;

    @Autowired
    private TouristOrgInfoMapper touristOrgInfoMapper;

    @Autowired
    private ITouristOrgBiz touristOrgBiz;

    // 机构提交审核必填字段
    private static final String[] CREATE_REQUIRED_FIELDS;

    static {
        CREATE_REQUIRED_FIELDS = new String[] {
                "bizCode", "orgName", "legalName", "cardType", "cardNo", "phoneNum", "idCardFrontPath",
                "idCardBackPath", "businessLicensePath", "roadTransLicensePath"
        };
    }

    @Override
    public ResponseBaseBean createTouristOrgForAudit(TouristOrgSnapReq touristOrgSnapReq) {
        ResponseBaseBean res = new ResponseBaseBean();
        if(touristOrgSnapReq == null) {
            log.warn("机构信息提交审核-请求参数为空");
            res.setCode(FrameworkErrorCodeEnum.PAPAM_IS_NULL.getCode());
            res.setMsg(FrameworkErrorCodeEnum.PAPAM_IS_NULL.getMsg());
            return res;
        }
        log.info("correctionId=[{}]-[机构信息提交审核]-开始-请求参数=[{}]", touristOrgSnapReq.getCorrelationID(), JSON.toJSONString(touristOrgSnapReq));
        // 必填参数校验
        try {
            ValidateUtil.requiredFieldsChk(touristOrgSnapReq, CREATE_REQUIRED_FIELDS);
            // 查询该组织机构编号的机构是不是已经在审核中或者审核通过
            int count = touristOrgInfoSnapMapper.countByBizCode(touristOrgSnapReq.getBizCode());
            if(count > 0) {
                log.warn("correlationID=[{}]-[机构信息提交审核]-存在还审核中或审核通过的记录-bizCode=[{}]", touristOrgSnapReq.getCorrelationID(), touristOrgSnapReq.getBizCode());
                res.setCode(FrameworkErrorCodeEnum.ORG_HAS_ALREADY_IN_SNAP.getCode());
                res.setMsg(FrameworkErrorCodeEnum.ORG_HAS_ALREADY_IN_SNAP.getMsg());
                return res;
            }
            // 新增机构审核信息和证照审核信息
            touristOrgBiz.createTouristOrgSnap(touristOrgSnapReq);
            res.setCode(FrameworkErrorCodeEnum.SUCCESS.getCode());
            res.setMsg(FrameworkErrorCodeEnum.SUCCESS.getMsg());
            log.info("correlationID=[{}]-[机构信息提交审核]-结束-[{}]", touristOrgSnapReq.getCorrelationID(), JSON.toJSONString(res));
        }  catch (BusinessException e) {
            log.error("correlationID=[{}]-[机构信息提交审核]-业务异常-code=[{}]-msg=[{}]", touristOrgSnapReq.getCorrelationID(), e.getErrorCode(), e.getErrorMessage(),
                    e);
            res.setCode(e.getErrorCode());
            res.setMsg(e.getErrorMessage());
            return res;
        } catch (Exception e) {
            log.error("correlationID=[{}]-[机构信息提交审核]-系统异常", touristOrgSnapReq.getCorrelationID(), e);
            res.setCode(FrameworkErrorCodeEnum.SYSTEM_ERR.getCode());
            res.setMsg(FrameworkErrorCodeEnum.SYSTEM_ERR.getMsg());
            return res;
        }
        return res;
    }
}
